#include "definiciones_globales.h"

SDL_Surface* CargarImagen(const char* filename){
    SDL_Surface* loaded_image = NULL;
    SDL_Surface* optimized_image = NULL;
    //loaded_image = SDL_LoadBMP(filename); // de SDL
    loaded_image = IMG_Load(filename);		// de la extension SDL_image carga BMP, PNM, XPM, LBM, PCX, GIF, JPEG, TGA y PNG
    if(loaded_image){
        optimized_image = SDL_DisplayFormat(loaded_image); // transforma al mismo formato que la screen, asi SDL no lo transforma auto. cada vez que hace blit (BMP es en 24 bit)
        SDL_FreeSurface(loaded_image);
        // hacer fondo transparente
        if(optimized_image){
            // Map the color key
            Uint32 colorkey = SDL_MapRGB(optimized_image->format,0,0xFF,0xFF);
            // Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey(optimized_image,SDL_SRCCOLORKEY,colorkey);
        }
        else
        {
			cout << "no existe la imagen ptimizada" << endl;
		}
    }
    else
    {
		cout << "no existe la imagen cargada" << endl;
	}

    return optimized_image;
}

void drawLineX(SDL_Surface* dst, int y, int x1, int x2 ){
	Uint32 colour = SDL_MapRGB( dst->format, 255, 0, 0 );
	for(int x=x1;x<x2;x++){
		Uint32* pixmem32 = (Uint32*) dst->pixels + y*(dst->w) + x;
		*pixmem32 = colour;
	}
}

void drawLineY(SDL_Surface* dst, int x, int y1, int y2 ){
	Uint32 colour = SDL_MapRGB( dst->format, 255, 0, 0 );
	for(int y=y1;y<y2;y++){
		Uint32* pixmem32 = (Uint32*) dst->pixels + y*(dst->w) + x;
		*pixmem32 = colour;
	}
}

void Error(const char* msg)
{
	cout << "Error: " << msg << endl;
	exit(1);
}
